<?php
namespace Crm\Table;

use Zend\Db\Sql\Select;
use Zend\Db\Sql\Where;
use Zend\Db\Sql\Expression;

class CityTable extends AbstractTable
{

    protected $table = 'hy_city';

    protected $id = 'city_id';
    
    public function find()
    {
        $rowset = $this->select();
        $result = $rowset->toArray();
        return $result;
    }

    public function findAll()
    {
        $rowset = $this->select();
        $result = $rowset->toArray();
        return $result;
    }

    public function findOne($id)
    {
        $rowset = $this->select(array(
            'city_id' => $id
        ));
        $row = $rowset->current();
        return $row;
    }

    public function create($data)
    {
        return $this->insert($data);
    }

    public function updateById()
    {}

    public function remove()
    {}

	public function updateItem($id,$data){
		if(empty($id)){
			throw new \Exception("id field is invalid!");
		}
		$data = array(
			'city_name'=>$data['city_name'],
			'update_time'=>new Expression('NOW()'),
		);
		return $this->update($data,array('city_id'=>$id));
	}
	
	public function deleteItem($id){
		if(empty($id)){
			throw new \Exception("id field is invalid!");
		}
		return $this->delete(array('city_id'=>$id));
		
	}
	
	public function addItem($data){
		
		$data = array(
			'city_name'=>$data['city_name'],
			'create_time'=>new Expression('NOW()'),
		    'update_time'=>new Expression('NOW()'),
		);
		

		$this->insert($data);
		$id = $this->lastInsertValue;

		return $id;
	}
	
	public function getItem($id){
		if(empty($id)){
			throw new \Exception("id field is invalid!");
		}
		$sql='SELECT * FROM `hy_city` WHERE `city_id` =:city_id';
		$statement=$this->getAdapter()->query($sql);
		$results=$statement->execute(array(':city_id'=>$id));
		$row=$results->current();
		return $row;
	}
	public function getCityList($query = array())
	{
	    $select = new Select(array('c' =>$this->getTable()));
	
	    $select->order('c.city_id');
	     
	    return $this->fetchPaginate($select,$query);
	}

}